package com.bonana.backend.services;

import com.bonana.core.dao.user.BackendUserDao;
import com.bonana.backend.session.BackendSession;
import com.bonana.core.dao.user.CoreUserDAO;
import com.bonana.core.dao.user.PartnerUserDao;
import com.bonana.core.entity.BackendUser;
import com.bonana.core.entity.CoreUser;
import com.bonana.core.entity.PartnerUser;
import com.bonana.core.result.Response;
import com.bonana.core.result.ResponseCode;
import com.bonana.utils.TimestampUtil;
import com.google.common.net.InetAddresses;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * Created by justin on 15/8/31.
 */
@Service
public class BackendLoginCheckService {
    private Logger logger = LogManager.getLogger(BackendLoginCheckService.class);
    @Autowired
    private BackendUserDao backendUserDAO;
    @Autowired
    private PartnerUserDao partnerUserDao;
    @Autowired
    private CoreUserDAO coreUserDao;

    public String encodePassword(String username, String password) {
        return DigestUtils.md5Hex(username + password);
    }

    public BackendUser queryByUsernameAndPassword(String username, String codePwd) {
        return backendUserDAO.queryByUsernameAndPassword(username, codePwd);
    }

    public PartnerUser queryByPartnerNameAndPassword(String username, String codePwd) {
        return partnerUserDao.queryByPartnerNameAndPassword(username, codePwd);
    }

    public CoreUser queryByBoBoUsernameAndPassword(String username, String codePwd) {
        return coreUserDao.queryUserByUsernameAndPassword(username, codePwd);
    }

    public Response validate(String username, String password, HttpSession session, String requestRemoteAddr) {
        String codePwd = encodePassword(username, password);
        BackendUser backendUser  = queryByUsernameAndPassword(username, codePwd);
        if (backendUser == null || backendUser.getUid() == 0) {
            PartnerUser partnerUser = queryByPartnerNameAndPassword(username, codePwd);
            if (partnerUser == null || partnerUser.getUid() == 0) {
                CoreUser boboUser = queryByBoBoUsernameAndPassword(username, codePwd);
                if (boboUser == null || boboUser.getUid() == 0) {
                    logger.warn("Login Failed: {} {} {}", username, password, requestRemoteAddr);
                    return new Response(ResponseCode.USERNAME_PASSWORD_ERROR);
                }
                BackendSession.saveSession(session, boboUser);
                logger.warn("Bobo Login : {} {} {}", username, password, requestRemoteAddr);
                return new Response(ResponseCode.SUCC_BOBOUSER);
            }
            if (partnerUser.getStatus() == 1) {
                return  new Response(ResponseCode.USER_DISABLED);
            }
            BackendSession.saveSession(session, partnerUser);
            partnerUserDao.updateLastLogin(username, InetAddresses.coerceToInteger(InetAddresses.forString(requestRemoteAddr)), TimestampUtil.now());
            logger.warn("Partner Login :{} {} {}", username, password, requestRemoteAddr);
            return new Response(ResponseCode.SUCC_PARTNER);
        }
        if(backendUser.getStatus() == 1) {
            return new Response(ResponseCode.USER_DISABLED);
        }
        backendUserDAO.updateLastLogin(username, InetAddresses.coerceToInteger(InetAddresses.forString(requestRemoteAddr)), TimestampUtil.now());
        BackendSession.saveSession(session, backendUser);
        logger.warn("Admin Login : {} {} {}", username, password, requestRemoteAddr);
        return new Response(ResponseCode.SUCC_ADMIN);
    }
}
